using System.Collections.Generic;
using Game.Config;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.EventSystems.EventTrigger;

public class ResourceManager
{
    static ResourceManager instance = new ResourceManager();
    public static ResourceManager Instance => instance;


    public T Load<T>(string path) where T : Object
    {

        return Resources.Load<T>(path);
    }

    public T Instantiate<T>(string path) where T : Object
    {
        var r = Load<T>(path);
        if(r != null)
        {
            return Object.Instantiate(r);
        }

        return null;
    }

    Stack<GameObject>hit_effect = new Stack<GameObject>(50);
    public GameObject Create_Hit_Effect(string path,Vector3 postion ,Vector3 forward)
    {
        GameObject result = null;
        if(hit_effect.Count >0)
        {
            var go = hit_effect.Pop();
            go.SetActive(true);
            result = go;
        }
        else
        {
            var obj =  Instantiate<GameObject>(path);
            Object.DontDestroyOnLoad(obj);//切场景不销毁
            result = obj;
        }

        if (result != null)
        {
            result.transform.position = postion;
            result.transform.forward = forward;
        }

        return result;

    }

    public void Destory(GameObject go)
    {
        Object.Destroy(go);
    }

    public void Destory_Hit_Effect(GameObject go)
    {
        if(go!=null)
        {
            go.SetActive(false);
            hit_effect.Push(go);
        }

    }


    public  Sprite LoadIcon(PropEntity propConfig)
    {
        if (propConfig.type == 0)
        {
            return Load<Sprite>($"{GameDefine.prop_root}{propConfig.icon}");
        }
        else if (propConfig.type == 1)
        {
            return Load<Sprite>($"{GameDefine.GetEQ_Icon(propConfig.part)}{propConfig.icon}");
        }
        else if (propConfig.type == 2)
        {
            return Load<Sprite>($"{GameDefine.mat_root}{propConfig.icon}");
        }
        Debug.LogError(propConfig.id + "  " + propConfig.type + "  " + propConfig.icon + "   " + propConfig.part);
        return null;

    }


    //对象池子
    Stack<GameObject> stack = new Stack<GameObject>();
    public void Destory_BagItem(GameObject o)
    {
        o.gameObject.SetActive(false);
        o.transform.SetParent(null,false);
        stack.Push(o);
    }

    public GameObject Create_BagItem(string item_path)
    {
        if(stack.Count == 0)
        {
            return Instantiate<GameObject>(item_path);
        }
        else
        {
            var obj=  stack.Pop();
            obj.SetActive(true);
            return obj;
        }
    }

    string item_path = "UI/Item/Prop_Item";
    public GameObject CreatePropItem(int id,int num =1)
    {
        var obj = ResourceManager.Instance.Instantiate<GameObject>(item_path);
        //更新道具图片
        Image icon = obj.transform.Find("icon").GetComponent<Image>();

        var propConfig = PropData.Get(id);
        var sprite = ResourceManager.Instance.LoadIcon(propConfig);
        if (sprite != null)
        {
            icon.sprite = sprite;
        }
        else
        {
            Debug.Log("背包物体路径有误，查看配置表");
        }
        obj.gameObject.name = propConfig.id.ToString();
        //这有个潜在的bug  数量 会有问题


        var count = obj.transform.Find("key/key_text").GetComponent<Text>();
        count.text = num.ToString();

        return obj;
    }

    string forgeItemPath = "UI/Item/Prop_Item";
    public GameObject CreatForgeItem()
    {
       return Instantiate<GameObject>(forgeItemPath);
    }
}
